package com.yanqu.road.entity.activity.jiuzhou.game;

import com.alibaba.fastjson.annotation.JSONField;
import com.yanqu.road.entity.activity.jiuzhou.helper.show.*;

import java.util.LinkedList;
import java.util.List;

/**
 * 战斗轮次战场数据
 */
public class CrossJiuZhouGameBattleData {

    /**
     * 所属房间
     */
    private int roomId;

    /**
     * 当前回合数
     */
    private int battleRound;

    /**
     * 用户1
     */
    private CrossJiuZhouGameBattleUserData battleUser1 = new CrossJiuZhouGameBattleUserData();

    /**
     * 用户2
     */
    private CrossJiuZhouGameBattleUserData battleUser2 = new CrossJiuZhouGameBattleUserData();

    /**
     * 是否结束
     */
    private boolean isFinish;


    // 战局表现数据,仅内存,不入库
    @JSONField(serialize = false)
    private CrossJiuZhouGameBattleShowData showData = null;

    /**
     * 开始记录
     */
    public void startRecord() {
        synchronized (this) {
            showData = new CrossJiuZhouGameBattleShowData();
            // 记录下当前的玩家信息
            CrossJiuZhouGameBattleRecordData originBattleData = new CrossJiuZhouGameBattleRecordData();
            originBattleData.setBattleRound(this.battleRound);
            originBattleData.setBattleUser1(this.battleUser1.clone());
            originBattleData.setBattleUser2(this.battleUser2.clone());
            showData.setOriginBattleData(originBattleData);
        }
    }

    /**
     * 添加步骤
     */
    public void addNewStep() {
        synchronized (this) {
            if (showData == null) {
                return;
            }
            CrossJiuZhouGameStepData stepData = new CrossJiuZhouGameStepData();
            stepData.setBattleRound(this.battleRound);
            showData.getStepDataList().addLast(stepData);
        }
    }

    /**
     * 添加技能效果
     *
     * @param skillEffectList 技能效果列表
     * @param type            1 战前  2战后  3血量变化 4 回合后
     */
    public void addSkillEffect(List<CrossJiuZhouSkillEffect> skillEffectList, int type) {
        if (showData == null || skillEffectList.isEmpty()) {
            return;
        }
        CrossJiuZhouGameStepData stepData = showData.getStepDataList().getLast();
        if (stepData == null) {
            return;
        }
        if (type == 1) {
            stepData.getBeforeSkillEffectList().addAll(skillEffectList);
        } else if (type == 2) {
            stepData.getAfterSkillEffectList().addAll(skillEffectList);
        } else if (type == 3) {
            LinkedList<CrossJiuZhouHpResult> hpResultList = stepData.getHpResultList();
            if (hpResultList != null && !hpResultList.isEmpty()) {
                CrossJiuZhouHpResult hpResult = hpResultList.getLast();
                if (hpResult != null) {
                    hpResult.getHpSkillEffectList().addAll(skillEffectList);
                }
            }
        } else if (type == 4) {
            stepData.getAfterRoundSkillEffectList().addAll(skillEffectList);
        }
    }

    /**
     * 添加门客战斗结果
     *
     * @param fightResult 门客战斗结果
     */
    public void addPatronsFightResult(CrossJiuZhouPatronsFightResult fightResult) {
        if (showData == null) {
            return;
        }
        CrossJiuZhouGameStepData stepData = showData.getStepDataList().getLast();
        if (stepData == null) {
            return;
        }
        stepData.getPatronsFightResults().add(fightResult);
    }

    /**
     * 添加血量结果
     */
    public void addHpResult(CrossJiuZhouHpResult hpResult) {
        if (showData == null) {
            return;
        }
        CrossJiuZhouGameStepData stepData = showData.getStepDataList().getLast();
        if (stepData == null) {
            return;
        }
        stepData.getHpResultList().add(hpResult);
    }

    /**
     * 停止记录
     */
    public void endRecord() {
        synchronized (this) {
            // 记录下当前的玩家信息
            CrossJiuZhouGameBattleRecordData resultBattleData = new CrossJiuZhouGameBattleRecordData();
            resultBattleData.setBattleRound(this.battleRound);
            resultBattleData.setBattleUser1(this.battleUser1.clone());
            resultBattleData.setBattleUser2(this.battleUser2.clone());
            showData.setResultBattleData(resultBattleData);
        }
    }

    public int getRoomId() {
        return roomId;
    }

    public void setRoomId(int roomId) {
        this.roomId = roomId;
    }

    public int getBattleRound() {
        return battleRound;
    }

    public void setBattleRound(int battleRound) {
        this.battleRound = battleRound;
    }

    public CrossJiuZhouGameBattleUserData getBattleUser1() {
        return battleUser1;
    }

    public void setBattleUser1(CrossJiuZhouGameBattleUserData battleUser1) {
        this.battleUser1 = battleUser1;
    }

    public CrossJiuZhouGameBattleUserData getBattleUser2() {
        return battleUser2;
    }

    public void setBattleUser2(CrossJiuZhouGameBattleUserData battleUser2) {
        this.battleUser2 = battleUser2;
    }

    public CrossJiuZhouGameBattleShowData getShowData() {
        return showData;
    }

    public boolean isFinish() {
        return isFinish;
    }

    public void setFinish(boolean finish) {
        isFinish = finish;
    }
}
